define([
    'jquery', 'underscore', 'backbone', 'marionette', 'router', 'vent', 'config',
    'view/storagelist-view', 'collection/storages',
    'view/itemlist-view', 'collection/items', 'view/historylist-view', 'collection/histories'
], function($, _, backbone, marionette, Router, vent, Config,
        StorageListView, Storages,
        ItemListView, Items, HistoryView, Histories) {

    function fetchAll() {
        var items = new Items();
        items.fetch({
            success: function(collection) {
                app.content.show(new ItemListView({
                    collection: collection
                }));
            },
            error: function(response) {
                alert("error: " + response);
            }
        });
    }
    function fetchStorages() {
        var storages = new Storages();
        storages.fetch({
            success: function(collection) {
                app.header.show(new StorageListView({
                    collection: collection
                }));
            },
            error: function(response) {
                alert("error: " + response);
            }
        });
    }
    function clearToken() {
        var storages = new Storages();
        storages.fetch({
            url: Config.authenUrl + '/cleartoken',
            success: function(collection) {
                app.header.show(new StorageListView({
                    collection: collection
                }));
            },
            error: function(response) {
                alert("error: " + response);
            }
        });
    }
    function openFolder(model) {
        var items = new Items();
        items.fetch({
            type: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            data: JSON.stringify(model.toPathParam()),
            success: function(collection) {
                histories.add(model);
                app.content.show(new ItemListView({
                    collection: collection
                }));
            },
            error: function(response) {
                alert("error: " + response);
            }
        });
    }




    var app = new marionette.Application();
    var histories = new Histories();
    app.addRegions({
        header: '#header',
        history: '#history',
        content: '#content'
    });
    app.addInitializer(function() {
        var view = new HistoryView({
            collection: histories
        });
        app.history.show(view);
    });
    var controller = {
        index: function() {
            vent.trigger('storagelist:render');
        }
    };
    new Router({
        controller: controller
    });
    vent.on('storagelist:render', function() {
        fetchStorages();
        fetchAll();
    });
    vent.on('storagelist:cleartoken', function() {
        clearToken();
        app.content.close();
    });
    vent.on('item:openFolder', openFolder);
    vent.on('history:click', function(model) {
        var push = false;
        var temp = [];
        histories.each(function(item) {
            if (model == item) {
                push = true;
            } else if (push) {
                temp.push(item);
            }
        });
        histories.remove(temp);
        if (model.get("id") == "")
            fetchAll();
        else
            openFolder(model);
    });
    backbone.history.start();
    return app;
});